import { createVNode, render, h } from 'vue';
import TipComponent from '@/components/LyTip/index.vue';

export type TipType = 'success' | 'warning' | 'info' | 'error';

interface TipOptions {
	message: string;
	type?: TipType;
	duration?: number;
}

const tip = (options: TipOptions | string) => {
	const container = document.createElement('div');

	const props = {
		...(typeof options === 'string' ? { message: options } : options),
		duration: (typeof options === 'string' ? 3000 : options.duration) ?? 3000,
		onDestroy: () => {
			render(null, container);
			document.body.removeChild(container);
		},
	};

	const vnode = createVNode(TipComponent, props);
	render(vnode, container);
	document.body.appendChild(container);
};

export default {
	success(message: string, duration?: number) {
		tip({ message, type: 'success', duration });
	},
	warning(message: string, duration?: number) {
		tip({ message, type: 'warning', duration });
	},
	info(message: string, duration?: number) {
		tip({ message, type: 'info', duration });
	},
	error(message: string, duration?: number) {
		tip({ message, type: 'error', duration });
	},
};
